<template>
  <view class="viewport">
    <!-- 导航栏 -->
    <view class="navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
      <navigator open-type="navigateBack" class="back icon-left" hover-class="none"></navigator>
      <view class="title">个人信息</view>
    </view>
    <!-- 头像 -->
    <view class="avatar">
      <view class="avatar-content">
        <image class="image" :src="userInfoDetail.avatar" mode="aspectFill" />
        <text class="text" @tap="editAvatar_btn">点击修改头像</text>
      </view>
    </view>
    <!-- 表单 -->
    <view class="form">
      <!-- 表单内容 -->
      <view class="form-content">
        <view class="form-item">
          <text class="label">账号</text>
          <text class="account">{{userInfoDetail.account}}</text>
        </view>
        <view class="form-item">
          <text class="label">昵称</text>
          <input class="input" type="text" placeholder="请填写昵称" v-model="editInfo.nickname" />
        </view>
        <view class="form-item">
          <text class="label">性别</text>
          <radio-group @change="radioChange">
            <label class="radio">
              <radio value="男" color="#27ba9b" :checked="editInfo.gender == '男'" />
              男
            </label>
            <label class="radio">
              <radio value="女" color="#27ba9b" :checked="editInfo.gender == '女'" />
              女
            </label>
          </radio-group>
        </view>
        <view class="form-item">
          <text class="label">生日</text>
          <picker class="picker" mode="date" start="1900-01-01" :end="new Date()" value="2000-01-01" @change="pickerChange">
            <view v-if="editInfo.birthday">{{editInfo.birthday}}</view>
            <view class="placeholder" v-else>请选择日期</view>
          </picker>
        </view>
        <view class="form-item">
          <text class="label">城市</text>
          <picker class="picker" mode="region" :value="['广东省', '广州市', '天河区']" @change="picker_city_change">
            <view v-if="editInfo.city">{{editInfo.city}}</view>
            <view class="placeholder" v-else>请选择城市</view>
          </picker>
        </view>
        <view class="form-item">
          <text class="label">职业</text>
          <input class="input" type="text" placeholder="请填写职业" v-model="editInfo.career" />
        </view>
      </view>
      <!-- 提交按钮 -->
      <button class="form-button" @tap="save_btn">保 存</button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { onLoad } from "@dcloudio/uni-app";
//引入仓库数据
import { useMemberStore } from "@/stores/index.ts";
const memberStore = useMemberStore();
//引入请求函数
import { reqUserInfo, reqUpdateUserInfo } from "@/services/profile";
//引入ts类型约束
import type { profileResponse } from "@/types/profile";


//查询系统距顶部安全距离
const { safeAreaInsets } = uni.getSystemInfoSync();
//uni的生命周期
onLoad(()=>{
    //获取当前用户的信息
    getUserInfo();
})

//当前服务器返回的用户信息
let userInfoDetail = reactive<profileResponse>({});

//获取用户信息
const getUserInfo = async () => {
  let res = await reqUserInfo();
  userInfoDetail = res.result;
  //给表单初始赋值
  editInfo.nickname = res.result.nickname;
  editInfo.gender = res.result.gender;
  editInfo.birthday = res.result.birthday;
  editInfo.city = res.result.fullLocation;
  editInfo.career = res.result.profession;
  console.log("用户信息api",res);
}

//收集用户修改的信息
const editInfo = reactive({
    nickname:"",
    gender:"",
    birthday:'',
    city:"",
    career:"",
    provinceCode:"",
    cityCode:"",
    countyCode:""
});
//radio单选框的change事件
const radioChange = (evt) => {
    editInfo.gender = evt.detail.value;
}
//生日选择picker的change事件
const pickerChange = (evt) => {
    editInfo.birthday = evt.detail.value;
}
//城市选择picker的change事件
const picker_city_change = (evt) => {
  editInfo.city = evt.detail.value.join("");
  editInfo.provinceCode = evt.detail.code[0];
  editInfo.cityCode = evt.detail.code[1];
  editInfo.countyCode = evt.detail.code[2];
}

//修改用户头像按钮
const editAvatar_btn = () => {
  //调用拍摄或从手机相册选择图片或视频
  uni.chooseMedia({
    count:1,
    mediaType:['image'],
    sourceType:['album', 'camera'],
    sizeType:['original', 'compressed'],
    camera:'front',
    success:(success)=>{
      const { tempFilePath } = success.tempFiles[0];  //本地临时路径
      //进行文件上传
      uni.uploadFile({
        url:'/member/profile/avatar',
        name:"file",
        filePath: tempFilePath,
        success:(success)=>{
          if(success.statusCode === 200){
            //提取头像
            const { avatar } = JSON.parse(res.data).result;
            userInfoDetail.avatar = avatar;
            memberStore.profile.avatar = avatar;
            uni.showToast({icon:"success",title:"更新成功!"});
          } else {
            uni.showToast({icon:"error",title:"更新失败,未知错误!"});
          }
        },
      })
    },
    fail:(fail)=>{
      uni.showToast({icon:"error",title:"您取消了选择!"});
    },
  })
}
//保存修改信息的按钮
const save_btn = async () => {
  //请求参数准备
  const obj = {
    nickname:editInfo.nickname,
    gender:editInfo.gender,
    birthday:editInfo.birthday,
    profession:editInfo.career,
    provinceCode:editInfo.provinceCode,
    cityCode:editInfo.cityCode,
    countyCode:editInfo.countyCode
  }
  //发请求更新信息
  let res = await reqUpdateUserInfo(obj);
  uni.showToast({icon:"success",title:"信息更新成功!"});
  console.log("用户信息更新api",res);
  //重新获取用户信息
  getUserInfo();
}

</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/order_bg.png);
  background-size: auto 420rpx;
  background-repeat: no-repeat;
}

// 导航栏
.navbar {
  position: relative;

  .title {
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    color: #fff;
  }

  .back {
    position: absolute;
    height: 40px;
    width: 40px;
    left: 0;
    font-size: 20px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

// 头像
.avatar {
  text-align: center;
  width: 100%;
  height: 260rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .text {
    display: block;
    padding-top: 20rpx;
    line-height: 1;
    font-size: 26rpx;
    color: #fff;
  }
}

// 表单
.form {
  background-color: #f4f4f4;

  &-content {
    margin: 20rpx 20rpx 0;
    padding: 0 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  &-item {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx;
    background-color: #fff;
    font-size: 28rpx;
    border-bottom: 1rpx solid #ddd;

    &:last-child {
      border: none;
    }

    .label {
      width: 180rpx;
      color: #333;
    }

    .account {
      color: #666;
    }

    .input {
      flex: 1;
      display: block;
      height: 46rpx;
    }

    .radio {
      margin-right: 20rpx;
    }

    .picker {
      flex: 1;
    }
    .placeholder {
      color: #808080;
    }
  }

  &-button {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 30rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #27ba9b;
  }
}
</style>